import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//使用callable，让线程带有返回值
public class ThreadDemo6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result = 0;
                for (int i = 0; i <= 1000 ; i++) {
                    result += i;
                }
                //不需要引入成员变量
                //直接借助这里的返回值即可
                return result;
            }
        };
        //引入Futuretask作为callable和Thread的粘合剂
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        System.out.println(futureTask.get());
    }
}
